﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace FYPHP
{
    public partial class SuccessTransactionaspx : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> ProductID = new List<string>();
            List<string> SoldQuantity = new List<string>();
            List<int> oldQuantity = new List<int>();
        
              
            SqlCommand cmdSuccessStatus,cmdUpdateSuccessQuantity,cmdSearchOldQuantity,cmdUpdateNewQuantity,cmdGetUserOldPoint,cmdSetUserNewPoint,cmdCheckCartOrBuyNow;
            SqlDataReader dtrUpdateSuccessQuantity;

            
   
         
              conn.Open();

            //Check transaction is cart or buy now
            cmdCheckCartOrBuyNow = new SqlCommand("Select t_status from Transactions Where t_id=@TransactionID", conn);
            cmdCheckCartOrBuyNow.Parameters.AddWithValue("@TransactionID", Session["TransactionID"].ToString());
            
            String TransactionStatus = cmdCheckCartOrBuyNow.ExecuteScalar().ToString();

            if (TransactionStatus.Equals("Cart"))
            {
                //Set the status to CartCompleted
                cmdSuccessStatus = new SqlCommand("Update Transactions Set t_status=@SuccessStatus  Where t_id=@TransactionID", conn);
                cmdSuccessStatus.Parameters.AddWithValue("@SuccessStatus", "CartCompleted");
                cmdSuccessStatus.Parameters.AddWithValue("@TransactionID", Session["TransactionID"].ToString());

                cmdSuccessStatus.ExecuteNonQuery();
            }
            else if (TransactionStatus.Equals("BuyNow"))
            {
                //Set the status to BuyNowCompleted
                cmdSuccessStatus = new SqlCommand("Update Transactions Set t_status=@SuccessStatus  Where t_id=@TransactionID", conn);
                cmdSuccessStatus.Parameters.AddWithValue("@SuccessStatus", "BuyNowCompleted");
                cmdSuccessStatus.Parameters.AddWithValue("@TransactionID", Session["TransactionID"].ToString());

                cmdSuccessStatus.ExecuteNonQuery();
            }

         

            //Obtained the transaction product with the transaction ID
            cmdUpdateSuccessQuantity = new SqlCommand("Select * from Transactions_Products Where t_id=@TransactionID",conn);
            cmdUpdateSuccessQuantity.Parameters.AddWithValue("@TransactionID", Session["TransactionID"].ToString());

            dtrUpdateSuccessQuantity = cmdUpdateSuccessQuantity.ExecuteReader();

                if(dtrUpdateSuccessQuantity.HasRows)
                {
                    while (dtrUpdateSuccessQuantity.Read())
                    {
                        //take out the sold product id and quantity
                        ProductID.Add(dtrUpdateSuccessQuantity["p_id"].ToString());
                        SoldQuantity.Add(dtrUpdateSuccessQuantity["quantity"].ToString());
                    }
                }
                
                dtrUpdateSuccessQuantity.Close();

                for (int i = 0; i < ProductID.Count; i++) // Loop through List with for
                {
                    //take out the old quantity
                    cmdSearchOldQuantity = new SqlCommand("Select p_quantity from Products where p_id=@ProductID", conn);
                    cmdSearchOldQuantity.Parameters.AddWithValue("@ProductID", ProductID[i].ToString());

                    oldQuantity.Add(Convert.ToInt32(cmdSearchOldQuantity.ExecuteScalar()));

                }

                for (int i = 0; i < ProductID.Count; i++) // Loop through List with for
                {
                    //update after minus oldquantity with the sold quantity
                    cmdUpdateNewQuantity = new SqlCommand("Update Products Set p_quantity = @NewQuantity where p_id=@ProductID", conn);
                    cmdUpdateNewQuantity.Parameters.AddWithValue("@ProductID", ProductID[i].ToString());
                    cmdUpdateNewQuantity.Parameters.AddWithValue("@NewQuantity",Convert.ToInt32(oldQuantity[i].ToString())-Convert.ToInt32(SoldQuantity[i].ToString()));
                    cmdUpdateNewQuantity.ExecuteNonQuery();
                }


                cmdGetUserOldPoint = new SqlCommand("Select u_points from Users where u_id=@UID ",conn);
                cmdGetUserOldPoint.Parameters.AddWithValue("@UID",Session["ID"].ToString());

                int UserCurrentPoint = Convert.ToInt32(cmdGetUserOldPoint.ExecuteScalar());


                cmdSetUserNewPoint = new SqlCommand("Update Users Set u_points = @NewPoints where u_id=@UID",conn);
                cmdSetUserNewPoint.Parameters.AddWithValue("@NewPoints", Convert.ToInt32(UserCurrentPoint) + Convert.ToInt32(Session["TotalOrder"].ToString()));
                cmdSetUserNewPoint.Parameters.AddWithValue("@UID", Session["ID"].ToString());

                cmdSetUserNewPoint.ExecuteNonQuery();

            conn.Close();

            MessageBox("You Have Successfully Complete The Transaction");
      
            Redirect("Default.aspx");

        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

    }
}